Imports System.data
Imports System.data.OleDb
Imports RIBS.Code.DBCode
Public Class dbUser

    Private _UserID As String
    Private _UserName As String

    Sub New(Optional ByVal vUserID As String = "", Optional ByVal AllowAdd As Boolean = True)
        Try
            If Trim(vUserID) = "" Then
                vUserID = Environ("USERNAME").ToUpper
            End If

            Dim Sql As String = "SELECT * FROM USers Where UserID  = '" & vUserID.ToString & "'"
            Dim con As New OleDbConnection(gConnectionString)
            Dim cmd As New OleDbCommand(Sql, con)
            Dim Reader As OleDbDataReader
            Dim NumLinkedItems As Int32 = 0
            con.Open()
            Reader = cmd.ExecuteReader(CommandBehavior.SingleRow)
            If Reader.Read() Then
                If vUserID = Environ("USERNAME").ToUpper Then
                    ExecuteNonQuery("UPDATE USERS SET LastLogin = '" & Now & "' WHERE UserID = '" & vUserID & "'")                    
                End If
                Reader.Close()
                cmd.CommandText = ("Select * From UserGroup Where UserID = '" & vUserID & "'")
                Reader = cmd.ExecuteReader(CommandBehavior.SingleRow)
                If Reader.Read() Then
                Else
                    ExecNoError("INSERT INTO UserGroup (UserID, GroupName) VALUES ('" & vUserID.ToUpper & "','Default')")
                End If
            Else
                If AllowAdd Then
                    ExecuteNonQuery("INSERT INTO USERS (UserID, UserName, LastLogin) VALUES ('" & vUserID.ToUpper & "','" & vUserID.ToUpper & "','" & Now & "')")
                    ExecNoError("INSERT INTO UserGroup (UserID, GroupName) VALUES ('" & vUserID.ToUpper & "','Default')")
                End If
            End If
            con.Close()
            Reader.Close()
            'cmd.CommandText = "SELECT * FROM USers Where UserID  = 'None'"
            'Reader.Close()
            'Reader = cmd.ExecuteReader(CommandBehavior.SingleRow)
            'If Not Reader.Read() Then
            '    ExecuteNonQuery("INSERT INTO USERS (LastLogin, UserID, UserName) " & _
            '                    "VALUES ('" & Now & "','None', 'None')")
            'End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Property UserID(ByVal vUserid As String) As String
        Get
            Try
                Return _UserID.ToUpper
            Catch ex As Exception
                Throw ex
            End Try
        End Get
        Set(ByVal value As String)
            Try
                _UserID = value
            Catch ex As Exception
                Throw ex
            End Try
        End Set
    End Property

    Public Property UserName(ByVal vUserid As String) As String
        Get
            Try
                Return _UserName.ToUpper
            Catch ex As Exception
                Throw ex
            End Try
        End Get
        Set(ByVal value As String)
            Try
                _UserName = value
            Catch ex As Exception
                Throw ex
            End Try
        End Set
    End Property

    Public Function GetAllUsers(Optional ByVal HideAdmin As Boolean = False) As DataTable
        Try
            Dim dt As New DataTable("USER")
            Dim Conn As New OleDbConnection(gConnectionString)
            Dim Adapter As New OleDbDataAdapter
            Dim cmd As New OleDbCommand
            Dim SQL As String = "SELECT * from Users "
            If HideAdmin Then
                SQL += " Where UserID not in ('Unallocated','Admin') " 'AND LastLogin >= " & "#" & Format(DateAdd(DateInterval.Month, -3, Date.Now), "dd MMM yyyy") & "#"
            Else
                SQL += "" ' Where LastLogin >= " & "#" & Format(DateAdd(DateInterval.Month, -3, Date.Now), "dd MMM yyyy") & "#"
            End If
            SQL += " ORDER BY USERID"

            Adapter.SelectCommand = New OleDbCommand(SQL, Conn)
            Adapter.Fill(dt)
            'Dim dr As DataRow
            'dr = dt.NewRow
            'dr("UserName") = ""
            'dt.Rows.Add(dr)
            Return dt
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
